﻿<?php ob_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta name="viewport" content="width=device-width user-scalable=no, target-densitydpi=medium-dpi" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Welcome to Yes or No!</title>
   	 <style type = "text/css">
			body{
				font-family: Futura;
				background-image:url("backgound.jpg");
				background-repeat: repeat;
				background-position: center;
			}
			p{
				color: maroon;
			}
			h1{
				font-family: Zapfino;
				color: gray;
				font-size : 40;
			
			}
		</style>
	<script type="text/javascript">
	window.onorientationchange = function() {
  
  var orientation = window.orientation;
  switch(orientation) {
    case 0:
       
        document.body.setAttribute("class","portrait");
       
       
        document.getElementById("currentOrientation").innerHTML="Now in portrait orientation (Home button on the bottom).";
        break; 
       
    case 90:
       
        document.body.setAttribute("class","landscapeLeft");
       
        document.getElementById("currentOrientation").innerHTML="Now in landscape orientation and turned to the left (Home button to the right).";
        break;
   
    case -90: 
       
        document.body.setAttribute("class","landscapeRight");
       
        document.getElementById("currentOrientation").innerHTML="Now in landscape orientation and turned to the right (Home button to the left).";
        break;
  }
}







</script>   


</head>
<body>
    <?php
		$error_msg ="";

        if( !isset( $_COOKIE['user_email'] ) ){
            if( isset( $_POST['login'] ) ){
                $dbc = mysqli_connect( 'localhost' ,'root' , '', 'teresa' ) or die( 'Error connecting to MySQL server.');
			
                $user_email = mysqli_real_escape_string( $dbc , trim($_POST['email']));
                $user_password = mysqli_real_escape_string( $dbc, trim($_POST['password']));

                if( !empty( $user_email ) && !empty( $user_password ) ){
                    $query = "SELECT email, first_name, user_id FROM teresa_user WHERE email = '$user_email' and password = SHA( '$user_password')";
                    $data = mysqli_query( $dbc , $query);
                    if( mysqli_num_rows( $data ) == 1 ){
                        $row = mysqli_fetch_array($data);
                        setcookie('user_email', $row[ 'email' ]);
                        setcookie('username', $row['first_name']);
						setcookie('user_id' , $row[ 'user_id' ] );
                        $home_url = 'http://' . $_SERVER['HTTP_HOST'] . '/~rlawjdxo1212/main.php';
						header('Location: ' . $home_url );
                    }else{
                        $error_msg = 'Sorry, you must enter a valid e-mail and password to log in.';
                    }
                }else{
                    $error_msg = 'Sorry, you must enter your e-mail and password to log in.';
                }
            }
        }
    ?>

    <h1>             Yes or No</h1>
	
	<?php
	
	if( empty( $_COOKIE['user_email'] ) ){
		echo '<p class = "error">' . $error_msg . '</p>';
	?>		
		<fieldset>
			<table>
				<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
			
			
 						<tr>
 							<td>
 								<p>E-mail ID:</p>
 							</td>
 							<td>
 								<input type="text" id="email" name="email" value = "<?php if( !empty( $user_email )) echo $user_email; ?>" /><br />
 							</td>
 						</tr>
						<tr>
							<td>
								<p>Password</p>
							</td>
							<td>
								<input type="password" id ="password" name= "password" maxlength="255"/><br />
							</td>
						</tr>
						<tr>
							<td></td>
							<td>	
								<input type="submit" value="Log In" name="login" maxlength="255" />
							</td>
						</tr>
				</form>

				<form method="post" action = "signup.php" >
					<tr>
						<td></td>
						<td>
							<input type ="submit" value = "Sign Up" name ="Sign Up" />
						</td>
					</tr>
				</form>
			</table>
		</fieldset>	
	<?php 
	}else{
		$home_url = 'http://' . $_SERVER['HTTP_HOST'] . '/~rlawjdxo1212/main.php';
		header('Location: ' . $home_url );
	}
	?>
</body>
</html>
